/*
 * Copyright 2012 Faculty of Informatics - Masaryk University.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cz.muni.fi.pa165.creatures.services.impl;

import cz.muni.fi.pa165.creatures.dao.CreatureTypeDAO;
import cz.muni.fi.pa165.creatures.dao.CreatureTypeWeaponDAO;
import cz.muni.fi.pa165.creatures.dto.CreatureTypeDTO;
import cz.muni.fi.pa165.creatures.dto.CreatureTypeWeaponDTO;
import cz.muni.fi.pa165.creatures.dto.mapping.CreatureTypeMapping;
import cz.muni.fi.pa165.creatures.dto.mapping.CreatureTypeWeaponMapping;
import cz.muni.fi.pa165.creatures.model.CreatureType;
import cz.muni.fi.pa165.creatures.model.CreatureTypeWeapon;
import cz.muni.fi.pa165.creatures.services.CreatureTypeService;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * This class deals with basic service operations regarding
 * {@link cz.muni.fi.pa165.creatures.model.CreatureType}.
 *
 * @author xvojtis2
 */
@Service
public final class CreatureTypeServiceImpl implements CreatureTypeService {

    private static final Logger logger =
            Logger.getLogger(CreatureTypeServiceImpl.class.getName());

    @Autowired
    private CreatureTypeDAO creatureTypeDAO;

    @Autowired
    private CreatureTypeMapping creatureTypeMapping;
    
     @Autowired
    private CreatureTypeWeaponMapping creatureTypeWeaponMapping;
     
     @Autowired
     private CreatureTypeWeaponDAO creatureTypeWeaponDAO;

    @Override
    @Transactional
    public void create(final CreatureTypeDTO creatureType) {
        try {
            creatureTypeDAO.create(
                    creatureTypeMapping.DTOtoEntity(creatureType));
        } catch (DataAccessException ex) {
            logger.log(Level.INFO, ex.getMessage());
        }
    }

    @Override
    @Transactional
    public void remove(final CreatureTypeDTO creatureType) {
        try {
            creatureTypeDAO.remove(
                    creatureTypeMapping.DTOtoEntity(creatureType));
        } catch (DataAccessException ex) {
             logger.log(Level.INFO, ex.getMessage());
            throw ex;
        }
    }

    @Override
    @Transactional
    public void update(final CreatureTypeDTO creatureType) {
        try {
            CreatureType crType = creatureTypeMapping.DTOtoEntity(creatureType);
            CreatureType crTypeDB = creatureTypeDAO.get(crType.getId());
            crTypeDB.setCreaturesInGroup(crType.getCreaturesInGroup());
            crTypeDB.setDefenseStrategy(crType.getDefenseStrategy());
            crTypeDB.setDescription(crType.getDescription());
            crTypeDB.setMaxHitPoints(crType.getMaxHitPoints());
            crTypeDB.setName(crType.getName());
            creatureTypeDAO.update(crTypeDB);
        } catch (DataAccessException ex) {
             logger.log(Level.INFO, ex.getMessage());
            throw ex;
        }
    }

    @Override
    @Transactional(readOnly = true)
    public CreatureTypeDTO getById(final Long id) {
        try {
            return creatureTypeMapping.entityToDTO(creatureTypeDAO.get(id));
        } catch (DataAccessException ex) {
             logger.log(Level.INFO, ex.getMessage());
            throw ex;
        }
    }

    @Override
    @Transactional(readOnly = true)
    public List<CreatureTypeDTO> getAll() {
        try {
            List<CreatureTypeDTO> creatureTypeDTOs =
                    new ArrayList<CreatureTypeDTO>();
            for (CreatureType c : creatureTypeDAO.getAll()) {
                creatureTypeDTOs.add(creatureTypeMapping.entityToDTO(c));
            }
            return creatureTypeDTOs;
        } catch (DataAccessException ex) {
             logger.log(Level.INFO, ex.getMessage());
            throw ex;
        }
    }

    @Override
    @Transactional(readOnly = true)
    public Long getCount() {
        try {
            return creatureTypeDAO.getCount();
        } catch (DataAccessException ex) {
             logger.log(Level.INFO, ex.getMessage());
            throw ex;
        }
    }

    @Override
    public List<CreatureTypeWeaponDTO> getWeapons(final CreatureTypeDTO creatureType) {
        try {
            CreatureType crType = creatureTypeMapping.DTOtoEntity(creatureType);
            CreatureType crTypeDB = creatureTypeDAO.get(crType.getId());
            if (crTypeDB == null) {
                return null;
            }
            List<CreatureTypeWeapon> weapons = crTypeDB.getWeapons();
            List<CreatureTypeWeaponDTO> results = new ArrayList<CreatureTypeWeaponDTO>();
            for (CreatureTypeWeapon weapon : weapons) {
                results.add(creatureTypeWeaponMapping.entityToDTO(weapon));
            }
            return results;
        } catch (DataAccessException ex) {
            logger.log(Level.INFO, ex.getMessage());
            throw ex;
        }
    }

    @Override
    @Transactional
    public void removeAll() {
        try {
            creatureTypeDAO.removeAll();
        } catch (DataAccessException ex) {
            logger.log(Level.INFO, ex.getMessage());
            throw ex;
        }
    }

    @Override
    @Transactional(readOnly=true)
    public CreatureTypeDTO getByName(String name) {
        try {
            return creatureTypeMapping.entityToDTO(creatureTypeDAO.getByName(name));
        } catch (DataAccessException ex) {
            logger.log(Level.INFO, ex.getMessage());
            throw ex;
        }
    }
}